if exists("g:vgdb_struct")
 finish
endif

let g:vgdb_struct = []

" 初始化之前唯一的接口
com VD call VgdbInit()

     
function VgdbStartServer()
    call libcall("vgdb.so", "VgdbStartServer", "")
endfunction


function! VgdbInit()
    if has_key(g:vgdb_struct,"state")
        finish
    endif

    call VgdbStartServer()
    command VQ call VgdbQuit()
    " 以后在这里加入特殊的语法高亮
    " 显示这个是vgdb命令执行ing, 需要等待一下
    tabnew | redraw | echo "vgdb try to get the entry..."
    " 我突然想起有个问题就是:
    " 发送的有序不能保证接收的有序
    " 唯一的解决办法就是发送端设定一个编号
    " 接收端如果先收到n+1, 则会先等待直到收到n并处理后, 再处理n+1
    " 在接收端弄一个循环队列, 专门搞这个
    
    call VgdbSendCmd("gdbcmd", "file ".a:0)
    call VgdbSendCmd("vimcmd", "SetCurrentFileAndDot")
    let g:vgdb_struct.state = 1
endfunction

function! VgdbQuit()
    call VgdbSendCmd("vimcmd", "Quit")
    echo "vgdb exits successfully."
endfunction


" vim所有的活动都是被动的, 被定死的
" 唯一一个能用来发送消息的就是:nbkey命令
" 服务器通过解析命令, 进行对应的动作
function! VgdbSendCmd(cmdtype, command)
    execute "nbkey ".cmdtype.":".g:vgdb_struct["seqno"].":".vgdbcmd
    let g:vgdb_struct["seqno"] += 1;
    " nbkey gdbcmd:12:file mapedit
    " nbkey gdbcmd:13:list
    " nbkey vimcmd:23:SetCurrentFileAndDot
endfunction
